<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Building_model extends CI_Model{
	private $name="building";
	public $buildingObj;
	public function __construct(){
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->database();
	}	
	public function setBuilding($id,$cond="1"){
	    $this->load->database();
        $sql = "SELECT * FROM ".$this->name." WHERE id=$id";
        $query=$this->db->query($sql);
        foreach($query->result() as $result){
            $this->load->model('building');		
			$this->buildingObj=$this->building;			
			$this->buildingObj->setBuilding($result->id,$result->group_id,$result->name,$result->city,$result->building_type_id,$result->ip);
        }
        $this->db->close();
	}
	public function get_statistics_daily($highest){
			for($param=0;$param <=$highest;$param++){				
				if($param==$highest){
					$sql = "SELECT SQL_CALC_FOUND_ROWS v.id
							FROM viewer v,viewer_log vl,wifi_location wl,wifi_building wb
							WHERE v.id=vl.userid and wl.id = v.channel AND wb.id = wl.buildingid $where
							AND vl.tstamp > curdate()
							";
					
				}else{
					$sql = "SELECT SQL_CALC_FOUND_ROWS v.id
							FROM viewer v,viewer_log vl,wifi_location wl,wifi_building wb
							WHERE v.id=vl.userid and wl.id = v.channel AND wb.id = wl.buildingid $where
							AND vl.tstamp > (date_sub(curdate(),interval ".($highest-$param)." day )) AND vl.tstamp < (date_sub(curdate(),interval ".($highest-($param+1))." day ))
							";
						
				}

				//echo $sql;
				$query = $this->db->query($sql);
				$res = $query->result();
				$x_data[$param]=date('Y-m-d',mktime(0, 0, 0, date("m")  , date("d")-($highest-$param), date("Y")));
				if($query->num_rows() <=0)
				{	$y_data[$param]=0;}else{
					$this->db->select("FOUND_ROWS() as row");
					$query = $this->db->get();
					$total = $query->row()->row;
					$y_data[$param]=(int)$total;
					if($y_data[$param] > $hNum){
						$hNum = $y_data[$param];
					}
				}
			}
			
			$data = array("title"=>"Daily active users","colour"=>"#DB1750","hNum"=>$hNum,"x_data"=>$x_data,"y_data"=>$y_data);
			
			return $data;		
	}
	public function get_building_list($format)
	{
		$sql="select id,name from building";
		$query=$this->db->query($sql);
		$results="";
		if($format=='select list'){
			foreach($query->result() as $result):
				$results.="<option id=\"building".$result->id."\" value='$result->id'>$result->name</option>";
			endforeach;
		}
		return $results;
	}
		public function get_building_type_list($format)
	{
		$sql="select id,name from building_type";
		$query=$this->db->query($sql);
		$results="";
		if($format=='select list'){
			foreach($query->result() as $result):
				$results.="<option id=\"building".$result->id."\" value='$result->id'>$result->name</option>";
			endforeach;
		}
		return $results;
	}
    function checkExistence($fieldname,$value){
        $sql = "SELECT * FROM ".$this->name." WHERE $fieldname = '$value'";
        $query=$this->db->query($sql);
        if ($query->num_rows() >0){
            return true;
        }
        return false;
    }
    function getValue($fieldname,$cond){
        $this->load->database();
        $sql = "SELECT ".$fieldname." as result FROM ".$this->name." WHERE ".$cond;
        $query=$this->db->query($sql);
        foreach($query->result() as $result){
            return $result->result;
        }
        $this->db->close();
        return "none";
    }
	function getType($fieldname,$cond){
        $this->load->database();
        $sql = "SELECT ".$fieldname." as result FROM building_type WHERE ".$cond;
        $query=$this->db->query($sql);
        foreach($query->result() as $result){
            return $result->result;
        }
        $this->db->close();
        return "none";
    }
    function getAll()
    {
        $query = $this->db->get($this->name);
        return $query->result();
    }
    function getBuildingId($id)
    {
        $this->load->database();
        $sql = "SELECT id as result FROM ".$this->name." WHERE group_id='$id'";
        $query=$this->db->query($sql);
        foreach($query->result() as $result){
            return $result->result;
        }
        $this->db->close();
        return "none";
    }
    function insert($data)
    {   
        $this->db->insert($this->name, $data);
    }

    function update($id,$data)
    {
        $this->db->update($this->name, $data, array($this->name.'_id' => $id));
    }

}